import { List, Map } from 'immutable'
import { INIT_MESSAGES, MARK_READED_BY_ID, MARK_READED_ALL } from '../actions/action-types'

const initialState = List([])

export default (state = initialState, { type, payload }) => {
  switch (type) {
    case INIT_MESSAGES:
      return List(payload.map(item => Map(item)))
    case MARK_READED_BY_ID:
      return state.map(item => {
        if (item.get('id') === payload.id) {
          return item.update('readed', readed => true)
        }
        return item
      })
    case MARK_READED_ALL:
      return state.map(item => item.update('readed', readed => true))
    default:
      return state
  }
}
